package com.moinut.studentattendancemanager.view.teacher;

import com.moinut.studentattendancemanager.UserManager;
import com.moinut.studentattendancemanager.controller.AttendanceController;
import com.moinut.studentattendancemanager.controller.ComboItemController;
import com.moinut.studentattendancemanager.controller.StudentController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.vo.AttendanceInfo;
import com.moinut.studentattendancemanager.model.vo.CourseInfo;
import com.moinut.studentattendancemanager.model.vo.db.Student;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.utils.WindowJumper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author MOILING
 */
public class InsertAttendanceFrame extends BaseFrame<InsertAttendanceFrame.InsertAttendancePanel> {
    private static final long serialVersionUID = 1L;

    private static final int DEFAULT_WIDTH = 310;
    private static final int DEFAULT_HEIGHT = 440;

    public InsertAttendanceFrame() {
        super("学生考勤信息录入", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected InsertAttendancePanel onSetContentPane() {
        return new InsertAttendancePanel();
    }

    /* package */ class InsertAttendancePanel extends JPanel implements ActionListener {
        private static final long serialVersionUID = 1L;

        private JPanel mTopPanel, mButtonPanel;
        private JLabel mPicLabel;
        private JLabel mClassLabel, mCourseLabel, mStudentIdLabel, mStudentNameLabel, mTimeLabel, mStateLabel;
        private JTextField mCourseTextField, mStudentNameTextField, mTimeTextField;
        private JComboBox<String> mClassComboBox, mStudentIdComboBox, mStateComboBox;
        private JButton mInsertButton, mBackButton;
        private List<CourseInfo> mCoursesInfo;
        private List<Student> mStudents;

        /*package*/ InsertAttendancePanel() {
            setLayout(new BorderLayout());
            initTopPanel();
            initButtonPanel();
            initData();
        }

        private void initData() {
            ComboItemController.getCourseByTeacherId(UserManager.userId, new OnGetListListener<CourseInfo>() {
                @Override
                public void onSuccess(List<CourseInfo> coursesInfo) {
                    InsertAttendancePanel.this.mCoursesInfo = coursesInfo;
                    for (CourseInfo courseInfo : coursesInfo) {
                        mClassComboBox.addItem(courseInfo.getClassId());
                    }
                    mClassComboBox.addItemListener(e -> {
                        mCoursesInfo.stream().filter(courseInfo ->
                                e.getItem().equals(courseInfo.getClassId())).forEach(courseInfo ->
                                mCourseTextField.setText(courseInfo.getCourseName()));
                        setStudentComboItems((String) e.getItem());
                    });
                    mClassComboBox.setSelectedItem(coursesInfo.get(0).getClassId());
                    mCourseTextField.setText(coursesInfo.get(0).getCourseName());
                    setStudentComboItems((String) mClassComboBox.getSelectedItem());
                    mStudentIdComboBox.addItemListener(e -> mStudents.stream()
                            .filter(student -> e.getItem().equals(student.getStudentId()))
                            .forEach(student -> mStudentNameTextField.setText(student.getStudentName())));
                }

                @Override
                public void onError(String error) {
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        private void setStudentComboItems(String classId) {
            StudentController.getStudentsByClass(classId, new OnGetListListener<Student>() {
                @Override
                public void onSuccess(List<Student> students) {
                    if (mStudents != null) mStudents.clear();
                    for (int i = mStudentIdComboBox.getItemCount() - 1; i >= 0; i--) {
                        mStudentIdComboBox.removeItemAt(i);
                    }
                    InsertAttendancePanel.this.mStudents = students;
                    for (Student student : students) {
                        mStudentIdComboBox.addItem(student.getStudentId());
                    }
                }

                @Override
                public void onError(String error) {
                    mStudents.clear();
                    for (int i = 0; i < mStudentIdComboBox.getItemCount(); i++) {
                        mStudentIdComboBox.removeItemAt(i);
                    }
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        private void initButtonPanel() {
            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mInsertButton = new JButton("录入考勤信息");
            mBackButton = new JButton("返回上级菜单");
            mButtonPanel.add(mInsertButton);
            mButtonPanel.add(mBackButton);
            add(mButtonPanel, BorderLayout.SOUTH);
            mBackButton.addActionListener(this);
            mInsertButton.addActionListener(this);
        }

        private void initTopPanel() {
            mTopPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            mClassLabel = new JLabel("教学班号");
            mCourseLabel = new JLabel("课程名称");
            mStudentIdLabel = new JLabel("学生ID");
            mStudentNameLabel = new JLabel("学生姓名");
            mTimeLabel = new JLabel("考勤时间");
            mStateLabel = new JLabel("考勤记录");
            mPicLabel = new JLabel();
            mPicLabel.setSize(DEFAULT_WIDTH, DEFAULT_WIDTH / 2);
            ImageIcon image = new ImageIcon(this.getClass().getResource("/res/images/login_head.jpg"));
            image.setImage(image.getImage().getScaledInstance(mPicLabel.getWidth(),
                    mPicLabel.getHeight(),
                    Image.SCALE_DEFAULT));
            mPicLabel.setIcon(image);
            mClassComboBox = new JComboBox<>();
            mCourseTextField = new JTextField();
            mCourseTextField.setEditable(false);
            mCourseTextField.setText("面向过程程序设计");
            mStudentIdComboBox = new JComboBox<>();
            mStudentNameTextField = new JTextField();
            mStudentNameTextField.setEditable(false);
            mStudentNameTextField.setText("凌霄");
            mTimeTextField = new JTextField();
            mStateComboBox = new JComboBox<>();
            mStateComboBox.addItem("正常");
            mStateComboBox.addItem("迟到");
            mStateComboBox.addItem("旷课");
            mStateComboBox.addItem("请假");

            mClassLabel.setPreferredSize(new Dimension(60, 26));
            mCourseLabel.setPreferredSize(new Dimension(60, 26));
            mStudentIdLabel.setPreferredSize(new Dimension(60, 26));
            mStudentNameLabel.setPreferredSize(new Dimension(60, 26));
            mTimeLabel.setPreferredSize(new Dimension(60, 26));
            mStateLabel.setPreferredSize(new Dimension(60, 26));

            mClassComboBox.setPreferredSize(new Dimension(220, 26));
            mCourseTextField.setPreferredSize(new Dimension(220, 26));
            mStudentIdComboBox.setPreferredSize(new Dimension(220, 26));
            mStudentNameTextField.setPreferredSize(new Dimension(220, 26));
            mTimeTextField.setPreferredSize(new Dimension(220, 26));
            mStateComboBox.setPreferredSize(new Dimension(220, 26));

            mTopPanel.add(mPicLabel);
            mTopPanel.add(mClassLabel);
            mTopPanel.add(mClassComboBox);
            mTopPanel.add(mCourseLabel);
            mTopPanel.add(mCourseTextField);
            mTopPanel.add(mStudentIdLabel);
            mTopPanel.add(mStudentIdComboBox);
            mTopPanel.add(mStudentNameLabel);
            mTopPanel.add(mStudentNameTextField);
            mTopPanel.add(mTimeLabel);
            mTopPanel.add(mTimeTextField);
            mTopPanel.add(mStateLabel);
            mTopPanel.add(mStateComboBox);
            add(mTopPanel, BorderLayout.CENTER);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(InsertAttendanceFrame.this);
            } else if (e.getSource().equals(mInsertButton)) {
                AttendanceController.insertAttendanceInfo(new AttendanceInfo(mTimeTextField.getText(),
                        (String) mStateComboBox.getSelectedItem(), (String) mStudentIdComboBox.getSelectedItem(),
                        (String) mClassComboBox.getSelectedItem(), mCourseTextField.getText()), new OnUpdateListener() {
                    @Override
                    public void onSuccess(String success) {
                        JOptionPane.showMessageDialog(null, success, "成功", JOptionPane.INFORMATION_MESSAGE);
                    }

                    @Override
                    public void onError(String error) {
                        JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                });
            }
        }
    }
}
